package thired.class01_sort;

public class Class03_InsertSort {
    public static void insertSort(int[] nums){
        if(nums == null || nums.length < 2){
            return;
        }
        for (int i = 1; i < nums.length; i++) {
            for (int j = i; j-1 >=0 && nums[j - 1] > nums[j]; j--) {
                swap(nums,j,j -1);
            }
        }

    }
    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    public static void main(String[] args) {
        int[] arr = new int[]{8,7,5,2,6,4,3,1,9};
        insertSort(arr);
        for (int i : arr) {
            System.out.print(i);
        }
    }
}
